﻿using FluentSilverlight.Binders;
using FluentSilverlight.Expressions;
using FluentSilverlight.MVVM;

namespace FluentSilverlight.Client.Screens.XamlSample
{
    public partial class SampleView : IOpinionatedControl<SampleViewModel>
    {
        public SampleView()
        {
            InitializeComponent();
        }

        public SampleViewModel Model
        {
            get { return DataContext as SampleViewModel; }
        }

        public void SetModel(IViewModel viewModel)
        {
            DataContext = viewModel;
        }

        public void Initialize()
        {
            this.WithTextBox(txtName)
                .Text(m => m.UserName)
                .IsReadOnly(m => m.UserNameIsReadOnly)
                .IsVisible(m => m.UserNameIsVisible)
                .IsEnabled(m => m.UserNameIsEnabled)
                .OnTextChange(m => m.UserNameChangedCommand);

            this.WithCheckBox(chkVisible).IsChecked(m => m.UserNameIsVisible);
            this.WithCheckBox(chkIsEnabled).IsChecked(m => m.UserNameIsEnabled);
            this.WithCheckBox(chkReadOnly).IsChecked(m => m.UserNameIsReadOnly);
            this.WithCheckBox(chkSaveCommandVisible).IsChecked(m => m.SaveCommandIsVisible);

            this.WithButton(btnSave)
                .IsVisible(m => m.SaveCommandIsVisible)
                .OnClick(m => m.SaveCommand);
            this.WithButton(btnCancel).OnClick(m => m.CancelCommand);

            // bind mode is redundand here since for a text block it is by default one way!
            this.WithTextBlock(lblStatus).Text(m => m.Status).BindMode.OneWay(); 
   
            this.WithTextBlock(lblEchoedUserName).Text(m => m.EchoedUserName);

            this.WithButton(btnTestVisibility)
                .IsVisible(m => m.VisibilityTestIsVisible)
                .OnClick(m => m.VisibilityTestCommand, true);

            this.WithCheckBox(chkTestVisibilityIsVisible).IsChecked(m => m.VisibilityTestIsVisible);
            this.WithCheckBox(chkTestVisibilityIsInteractable).IsChecked(m => m.VisibilityTestIsInteractable);

            this.WithButton(btnTestVisibility2)
                .IsVisible(m => m.VisibilityTest2IsVisible)
                .OnClick(m => m.VisibilityTest2Command, false);

            this.WithCheckBox(chkTestVisibility2IsVisible).IsChecked(m => m.VisibilityTest2IsVisible);
            this.WithCheckBox(chkTestVisibility2IsInteractable).IsChecked(m => m.VisibilityTest2IsInteractable);
        }
    }
}